package com.xbongbong.paas.script.controller;

import com.alibaba.fastjson.JSON;
import com.xbongbong.paas.script.pojo.dto.SynchronizationDTO;
import com.xbongbong.paas.toolbox.exception.XbbException;
import com.xbongbong.paas.toolbox.wrap.BaseVO;
import com.xbongbong.paas.toolbox.wrap.XbbResponse;
import com.xbongbong.pro.constant.XbbProConstant;
import com.xbongbong.pro.enums.errorcodes.SystemErrorCodeEnum;
import com.xbongbong.pro.message.pojo.dto.ConsistencySyncRabbitMqDTO;
import com.xbongbong.pro.rabbitmq.producer.ConsistencySyncProducer;
import com.xbongbong.pro.script.pojo.dto.ProductDTO;
import com.xbongbong.saas.enums.XbbRefTypeEnum;
import com.xbongbong.saas.service.RepairReceiptOrderService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.MediaType;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.Objects;

/**
 * @author 吴峰
 * @date 2020/11/25 15:19
 */
@RestController
@RequestMapping(XbbProConstant.API_ROOT_PREFIX + "/script/data")
public class DataSynchronizationController {

    private static final Logger LOGGER = LoggerFactory.getLogger(DataSynchronizationController.class);

    @Resource
    private ConsistencySyncProducer consistencySyncProducer;
    @Resource
    private RepairReceiptOrderService repairReceiptOrderService;

    @RequestMapping(value = "/synchronization", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String synchronization(@RequestBody @Valid SynchronizationDTO synchronizationDTO, BindingResult br) throws XbbException {
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            XbbResponse<BaseVO> response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
            return JSON.toJSONString(response);
        } else {
            ConsistencySyncRabbitMqDTO consistencySyncRabbitMqDTO = new ConsistencySyncRabbitMqDTO(synchronizationDTO.getCorpid(), synchronizationDTO.getId(), synchronizationDTO.getOpObjectName());
            try {
                XbbRefTypeEnum xbbRefTypeEnum = XbbRefTypeEnum.getByCode(synchronizationDTO.getBusinessType());
                consistencySyncProducer.syncData(consistencySyncRabbitMqDTO, xbbRefTypeEnum);
            } catch (XbbException e) {
                throw e;
            }catch (Exception e) {
                LOGGER.error("数据同步出错", e);
                return "fail";
            }
        }

        return "success";
    }
    @RequestMapping(value = "/repairReceiptOrderPrintExplain", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String repairReceiptOrderPrintExplain(@RequestBody @Valid ProductDTO productDTO, BindingResult br) throws XbbException {
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            XbbResponse<BaseVO> response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
            return JSON.toJSONString(response);
        } else {
            try {
                repairReceiptOrderService.repairReceiptOrderPrintExplain(productDTO);
            } catch (XbbException e) {
                throw e;
            }catch (Exception e) {
                LOGGER.error("数据同步出错", e);
                return "fail";
            }
        }

        return "success";
    }
}
